package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.mapper.CheckItemMapper;
import com.itcast.pojo.CheckItem;
import com.itcast.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    //检查项分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page = checkItemMapper.findPage(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //根据id删除检查项
    @Override
    public void delete(Integer id) {
        //判断是否有多表关联
        Long count = checkItemMapper.deleteCountById(id);
        if (count>0){
            //不能删除
            throw new RuntimeException();
        }
        checkItemMapper.deleteById(id);
    }
    //编辑检查项
    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }
    //回显数据
    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    //查询所有
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }
}
